package com.wyp168.jse;

/**
 * @ClassName ArithmeticTripletsSolution
 * @Title 算数三元组数组
 * @Description TODO
 * @Author wyp168
 * @Date 2023/3/31 22:28
 */
public class ArithmeticTripletsSolution {
    public static void main(String[] args) {
        ArithmeticTripletsSolution solution = new ArithmeticTripletsSolution();
        System.out.println(solution.arithmeticTriplets(new int[]{4, 5, 6, 7, 8, 9}, 2));
    }
    public int arithmeticTriplets(int[] nums, int diff) {
        boolean[] isUsed = new boolean[nums.length];
        dfs(0, nums, isUsed, 0, diff);
        return count;
    }

    int count = 0;

    int[] tempArr = {-1, -1, -1};

    public void dfs(int index, int[] nums, boolean[] isUsed, int i1, int diff) {

        if (index == 3) {
            if (nums[tempArr[1]] - nums[tempArr[0]] == diff
                    && nums[tempArr[2]] - nums[tempArr[1]] == diff) {
                count++;
            }
            return;
        }

        for (int i = i1; i < nums.length; i++) {
            if (!isUsed[i]) {
                tempArr[index++] = i;
                isUsed[i] = true;
                dfs(index, nums, isUsed, i + 1, diff);
                isUsed[i] = false;
                tempArr[--index] = -1;
            }

        }
    }
}
